import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ViewController} from 'ionic-angular';
import {StorageProvider} from "@providers/storage";
import {DialogProvider} from "@providers/dialog";
import {WorkerProvider} from "@providers/interface/worker";

@IonicPage()
@Component({
    selector: 'page-rpay-image',
    templateUrl: 'rpay-image.html',
})
export class RpayImagePage {
    userInfo: any;
    cacheOrder: any = this.navParams.data;
    _data = this.navParams.data;
    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                private storage: StorageProvider,
                private dialog: DialogProvider,
                private viewCtrl: ViewController,
                private worker: WorkerProvider) {
        this.userInfo = this.storage.read("userInfo");
    }

    push(page: string, data?: any) {
        this.navCtrl.push(page, data);
    }

    ionViewDidLoad() {
        this.dialog.hideLoading();
        console.log(this.cacheOrder);
    }

    dismiss(data?: any) {
        this.viewCtrl.dismiss(this.cacheOrder);
    }

    /*
      //微信支付
      weixinPay(data = 'wxPay') {
        this.dialogService.showLoading();
        //生成数据
        this.setOrderMx('1');
        /!*let _url = location.protocol + '//' + location.host;
         !Util.isNull(THE_SECONDARY_DOMAIN_NAME) && (_url += THE_SECONDARY_DOMAIN_NAME);
         this.dialogService.hideLoading();
         location.href = _url + "/appPay_ysw.html";*!/

        var pub_url = APP_SERVE_URL;		//后台接口程序
        var pub_url2 = location.origin + location.pathname;		//前台访问程序

        debugger;
        if (!sessionStorage.getItem("ordermx")) {
          alert("订单不存在或已提交!");
          location.href = pub_url2 + "/index.html";
        }

        var orderData = JSON.parse(sessionStorage.getItem("ordermx"));
        if (!orderData && orderData["PAYTYPE"] && orderData["PARTYID"] && orderData["orderId"]
          && orderData["body"] && orderData["openid"] && orderData["total_fee"]) {
          alert("订单数据错误!");
          return;
        }

        let onBridgeReady = () => {
          debugger
          var ajaxObj = {async: true};
          var json = {
            "PAYTYPE": orderData.PAYTYPE,					//支付类型"1"为微信支付
            "PARTYID": orderData.PARTYID,					//商家partyId
            "out_trade_no": orderData.orderId,				//商户单号
            "body": orderData.body,							//商品名称\描述
            "openid": orderData.openid,						//openid
            "total_fee": (orderData.total * 100).toFixed(0),			//总金额（单位分）
            "attach": orderData.attach,						//额外自定义参数
          };

          this.http.post(pub_url + "/trust/protoWeChatOnline/getConfigToPay.do", {
            "json": JSON.stringify(json)
          }).subscribe(data => {
            debugger
            var data = data.data;
            if (data && data.flag == 1) {
              WeixinJSBridge.invoke('getBrandWCPayRequest', {
                "appId": data.appId,
                "timeStamp": data.timeStamp,
                "nonceStr": data.nonceStr,
                "package": data.package,
                "signType": "MD5",
                "paySign": data.paySign
              }, res => {
                debugger
                sessionStorage.setItem("ordermx", JSON.stringify({"orderId": orderData.orderId}))
                if (res.err_msg == "get_brand_wcpay_request:ok") {			//"没有此SDK或暂不支持此SDK模拟"
                  sessionStorage.removeItem("ordermx");
                  this.navCtrl.push('TipSuccessPage', {
                    'title': '支付成功',
                    'desc': '您的订单已支付成功！',
                    'btn1': '继续购买',
                    'btn2': '查看订单',
                    'page1': 'root',
                    'page2': 'OrderPage'
                  });
                  this.dismiss();
                } else {
                  alert("支付失败!");
                }
              });
            } else {
              alert("支付参数获取失败! " + JSON.stringify(data));
            }
          });
        }
      }*/
    /**
     * 微信扫码支付
     */
    sucess() {
        this.cacheOrder.azlist = this.storage.read("azlist") ? this.storage.read("azlist") : [];
        this.worker.wandan(this._data).then( v => {
          this.dialog.hideLoading();
          }
        ).catch( e => {
          this.dialog.hideLoading();
          }
        );
        // this.http.post("/Inbound/invoke.do", {
        //     interfaceId: "csm.som.ext.cssOrder.createOrder",
        //     data: JSON.stringify({
        //         'partyId': this.cacheOrder.partyId,
        //         'SUM': this.cacheOrder.sum,
        //         'CustomerName': this.cacheOrder.name,
        //         'CustomerPhone': this.cacheOrder.phone,
        //         'bz': this.cacheOrder.bz,
        //         'costs': this.cacheOrder.costs,
        //     })
        // }).subscribe(message => {
        //     this.dialog.hideLoading();
        //     console.info(message, 1);
        //     console.info("完成");
        //     this.dismiss(message);
        //     //this.push("RchargePage");
        // }, error => {
        //     this.dialog.hideLoading();
        //     this.dialog.showAlert("完工失败:" + String(error));
        //     this.dismiss()
        // });
    }
}




